package whf.framework.config;

import java.net.URL;
import java.util.Map;

import org.apache.commons.beanutils.Converter;

import whf.framework.util.StringUtils;
import whf.framework.util.Utils;
import whf.framework.web.struts.converters.BaseEnumToStringConverter;

/**
 * 单例类，应用配置，从conf/application.conf.xml中读取配置数据
 * @author wanghaifeng
 * @create Nov 29, 2006 12:45:46 PM
 * 
 */
public final class ApplicationConfig {
	private static ApplicationConfig instance = new ApplicationConfig();
	
	private Configuration config ;
	
	private ApplicationConfig(){
		try{
			URL url = ApplicationConfig.class.getClassLoader().getResource("conf/application.conf.xml");
			if(url != null){
				this.config = new XmlConfiguration(url);
			}
		}catch(Exception e){
			throw new IllegalArgumentException(e);
		}
	}
	
	public final static ApplicationConfig getInstance(){
		return instance;
	}
	
	/**
	 * 是否是产品模式
	 * 是：启用若干cache
	 * @modify wanghaifeng Dec 27, 2006 11:08:05 AM
	 * @return
	 */
	public final boolean isProductMode(){
		return StringUtils.equalsIgnoreCase(getValue("application.product-mode"), "product");
	}
	
	/**
	 * 根据键获取配置值
	 * @modify wanghaifeng Nov 29, 2006 1:40:08 PM
	 * @param key
	 * @return
	 */
	public String getValue(String key){
		return this.config.getString(key);
	}
	
	public String[] getValues(String key){
		return this.config.getStringArray(key);
	}
	
	/**
	 * 获取修饰类类名
	 * @modify wanghaifeng Nov 29, 2006 1:37:15 PM
	 * @param className
	 * @return
	 */
	public String getWrapperClass(String className){
		className = StringUtils.replace(className, ".", "_");
		return this.getValue("class-wrapper."+className);
	}
	
	/**
	 * 获取上传文件的路径
	 * @modify wanghaifeng Nov 29, 2006 1:39:42 PM
	 * @return
	 */
	public String getUploadDirectory(){
		return this.getValue("file.upload");
	}
	
	public String getReportTemplateDirectory() {
		return this.getValue("file.reportTemplate");
	}
	
	public String getIconPath() {
		return this.getValue("file.icon");
	}
	
	/**
	 * @return 返回设置的web路径
	 */
	public String getContextPath() {
		return this.getValue("application.context");
	}
	
	/**
	 * 国际化资源信息
	 * @modify wanghaifeng Apr 17, 2007 11:26:49 PM
	 * @return
	 */
	public String getI18n(){
		return this.getValue("resource.i18n");
	}
	
	/**
	 * 获取Spring配置文件
	 * @modify wanghaifeng Nov 30, 2006 12:05:08 AM
	 * @return
	 */
	public String[] getSpringConfigurationFiles(){
		return getValues("spring-files.file");
	}
	
	/**
	 * 获取Struts的类型转换器
	 * @modify wanghaifeng Dec 26, 2006 3:42:16 PM
	 * @return
	 */
	public Map<Converter, Class> getStrutsConverters(){
		Map<Converter, Class> map = Utils.newHashMap();
		String[] clses = getValues("struts.converters.classes.class");
		for(String clazz: clses){
			Class c = Utils.getClassByClassName(clazz);
			if(c == null) continue;
			String converterClassName = this.getValue("struts.converters.converters." + StringUtils.replace(clazz, ".", "_"));
			Class converterClass = Utils.getClassByClassName(converterClassName);
			Converter converter = null;
			if(converterClass == null) {
				if(c.isEnum()){
					converter = new BaseEnumToStringConverter();
				}
			} else {
				converter = (Converter)Utils.constructBean(converterClass);
			}
			if(converter == null) continue;
			map.put(converter, c);
		}
		return map;
	}
	
	public String getEncoding(){
		return "UTF-8";
	}
	
	public boolean isServiceMonitoring() {
		return "true".equalsIgnoreCase(this.getValue("monitor.service"));
	}
	
	public static void main(String[] args) throws Exception{
		System.out.println(ApplicationConfig.getInstance().getValue("class-wrapper.whf_framework_auth_bo_Permission"));
		String[] files = ApplicationConfig.getInstance().getValues("spring-files.file");
		for(String f: files){
			System.out.println(f);
		}
		//
		Map<Converter, Class> map = ApplicationConfig.getInstance().getStrutsConverters();
		for(Converter c: map.keySet()){
			System.out.println(c+"\t"+map.get(c));
		}
	}
}
